package com.snake.window;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 2 * @Author: Conph
 * 3 * @Date: 2019/7/28 21:19
 * 4
 */
public class MainView implements SwingView {
    private JFrame mainView;
    private Container container;
    private static final int PADDING = 10;
    private static final int VIEW_WIDTH = 900;
    private static final int VIEW_HEIGHT = 700;
    private static final int VIEW_X = 300;
    private static final int VIEW_Y = 100;


    private JPanel gameArea;
    private static final int GAME_VIEW_WIDTH = 900;
    private static final int GAME_VIEW_HEIGHT = 600;
    private static final int GAME_VIEW_X = 0;
    private static final int GAME_VIEW_Y = 0;
    private Color gameAreaColor = new Color(142, 205, 227);

    private JButton jbtnControl;
    private static final int JBTN_HEIGHT = 60;
    private static final int JBTN_WIDTH = 120;
    private Font JBTN_FONT = new Font("宋体", 0, 30);

    private static final int BLOCK_WITH = 10;
    private static final int BLOCK_HEIGHT = 10;

    private List<JButton> wallList;
    private Color wallColor = new Color(158, 95, 60);
    private int wallWidthBlockCount = 84;
    private int wallHeightBlockCount = 55;
    private int wallX = PADDING * 2;
    private int wallY = 0;

    public MainView() {
        initView();
        reinitView();
        addEvent();
    }

    @Override
    public void initView() {
        mainView = new JFrame("贪吃蛇小游戏");
        gameArea = new JPanel();
        gameArea.setLayout(null);
        gameArea.setBounds(GAME_VIEW_X, GAME_VIEW_Y, GAME_VIEW_WIDTH, GAME_VIEW_HEIGHT);
        gameArea.setBackground(gameAreaColor);

        container = mainView.getContentPane();
        container.add(gameArea);
        mainView.setLayout(null);
        mainView.setBounds(VIEW_X, VIEW_Y, VIEW_WIDTH, VIEW_HEIGHT);

        jbtnControl = new JButton("开始");
        jbtnControl.setBounds((GAME_VIEW_WIDTH - JBTN_WIDTH) / 2,
                GAME_VIEW_HEIGHT, JBTN_WIDTH, JBTN_HEIGHT);
        jbtnControl.setFont(JBTN_FONT);
        container.add(jbtnControl);

        wallList = new ArrayList<JButton>();
    }

    @Override
    public void reinitView() {
        mainView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        drawWall(wallX, wallY, wallWidthBlockCount, wallHeightBlockCount);

    }

    @Override
    public void addEvent() {
    }

    @Override
    public void showView() {
        mainView.setVisible(true);
    }

    @Override
    public void closeView() {
        mainView.setVisible(false);
    }

    /*
    * 搭建墙
    * */
    private void drawWall(int wallX, int wallY, int wallWidthBlockCount, int wallHeightBlockCount) {
        int curX = wallX;
        int curY = wallY;

        for(int count = 0; count < wallWidthBlockCount; count++) {
            wallList.add(drawOneBlock(curX, curY, wallColor));
            wallList.add(drawOneBlock(curX, curY + wallHeightBlockCount * BLOCK_HEIGHT, wallColor));
            curX += BLOCK_WITH;
        }

        curX = wallX;
        curY = wallY;

        for(int count = 0; count <= wallHeightBlockCount; count++) {
            wallList.add(drawOneBlock(curX, curY, wallColor));
            wallList.add(drawOneBlock(curX + wallWidthBlockCount * BLOCK_WITH, curY, wallColor));
            curY += BLOCK_HEIGHT;
        }

    }

    public JButton drawOneBlock(int blockX, int blockY, Color blockColor) {
        JButton block = new JButton();
        block.setBounds(blockX, blockY, BLOCK_WITH, BLOCK_HEIGHT);
        block.setBackground(blockColor);
        block.setVisible(true);
        gameArea.add(block);
        block.setEnabled(false);

        return block;
    }

    public JPanel getGameArea() {
        return gameArea;
    }

    public JButton getJbtnControl() {
        return jbtnControl;
    }

    public List<JButton> getWallList() {
        return wallList;
    }

    public int getWallWidthBlockCount() {
        return wallWidthBlockCount;
    }

    public void setWallWidthBlockCount(int wallWidthBlockCount) {
        this.wallWidthBlockCount = wallWidthBlockCount;
    }

    public int getWallHeightBlockCount() {
        return wallHeightBlockCount;
    }

    public void setWallHeightBlockCount(int wallHeightBlockCount) {
        this.wallHeightBlockCount = wallHeightBlockCount;
    }

    public int getWallX() {
        return wallX;
    }

    public void setWallX(int wallX) {
        this.wallX = wallX;
    }

    public int getWallY() {
        return wallY;
    }

    public void setWallY(int wallY) {
        this.wallY = wallY;
    }

    public int getBlockWith() {
        return BLOCK_WITH;
    }

    public int getBlockHeight() {
        return BLOCK_HEIGHT;
    }

    public int getGameViewWidth() {
        return GAME_VIEW_WIDTH;
    }

    public int getGameViewHeight() {
        return GAME_VIEW_HEIGHT;
    }

}
